/*
 * Copyright (c) 2016. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.tsinghuabigdata.edu.ddmath.commons.image;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

import com.tsinghuabigdata.edu.ddmath.util.AlertManager;


/**
 * 图片资源
 * Created by yanshen on 2016/4/7.
 */
public class ImageResource {

    private Activity mContext;

    public ImageResource(Activity context) {
        this.mContext = context;
    }

    /**
     * 调用相册
     *
     * @param requestCode
     */
    public void openPhotoSel(int requestCode) {
        Intent intent = new Intent();
        /* 开启Pictures画面Type设定为image */
        intent.setType("image/*");
        /* 使用Intent.ACTION_GET_CONTENT这个Action */
        intent.setAction(Intent.ACTION_GET_CONTENT);
        mContext.startActivityForResult(Intent.createChooser(intent, "请选择上传头像"), requestCode);
    }

    /**
     * 调用相册
     *
     * @param requestCode
     */
    public void openPhotoSel2(int requestCode) {
        Intent intent = new Intent();

        /* 使用Intent.ACTION_GET_CONTENT这个Action */
        intent.setAction(Intent.ACTION_PICK);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
        mContext.startActivityForResult(Intent.createChooser(intent, "请选择上传头像"), requestCode);
    }

    /**
     * 调用图片剪辑程序
     *
     * @param uri
     * @param requestCode
     */
    public void openPhotoClip(Uri uri, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 200);
        intent.putExtra("outputY", 200);
        intent.putExtra("return-data", true);
        mContext.startActivityForResult(intent, requestCode);
    }

    /**
     * 调用相机
     *
     * @param requestCode
     */
    public void openCamera(Uri uri, int requestCode) {
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))// 判断是否有SD卡
        {
            AlertManager.toast(mContext, "没有SD卡");
            return;
        }
        // 调用照相机
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, null);
        // 设置保存文件路径
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        mContext.startActivityForResult(intent, requestCode);
    }
}
